$(document).ready(function(){
	// 字符验证       
	jQuery.validator.addMethod("stringCheck", function(value, element) {       
		return this.optional(element) || /^\w+$/.test(value);       
	}, "只能包括英文字母、数字和下划线");
	// 手机号码验证       
	jQuery.validator.addMethod("isMobile", function(value, element) {       
		var length = value.length;   
		var mobile = /^(13[0-9]|15[0-9]|18[0-9]|14[0-9])\d{8}$/;   
		return this.optional(element) || (length == 11 && mobile.test(value)); 
	}, "请正确填写您的手机号码");
	// default validate form on keyup and submit
	var validator = $("#reg_form").validate({
		rules: {
			username: {
				required: true,
				minlength: 4,
				maxlength:20,
				stringCheck:true
			},
			password: {
				required: true,
				minlength: 8,
				maxlength:20,
				stringCheck:true
			},
			confirm_password: {
				required: true,
				minlength: 8,
				maxlength:20,
				stringCheck:true,
				equalTo: "#password"
			},
			email: {
				required: true,
				email: true
			},
			telephone: {
				required:true,
				isMobile:true
			}
		},
		messages: {
			username: {
				required:"请输入用户名",
				minlength:"用户名不能少于4个字符",
				maxlength:"用户名不能超过20个字符"
			},
			password: {
				required:"请输入密码",
				minlength: jQuery.validator.format("密码不能少于 {0}个字符"),
				maxlength:jQuery.validator.format("密码不能超过 {0}个字符")
			},
			confirm_password: {
				required: "请输入密码",
				minlength: jQuery.validator.format("密码不能少于 {0}个字符"),
				equalTo: "两次输入的密码要一致"
			},
			email: {
				required: "请输入正确的邮箱地址",
				email: "请输入正确的邮箱地址"
			},
			telephone:{
				required:"手机号码必须要填写",
				isMobile:"请输入正确的手机号码"
			}
		},
		highlight: function(element) {
			$(element).removeClass('success').addClass('error');
			//当某个input验证成功后再失败的话，添加的ok类依然存在
			$(element).parent().find('label.ok').removeClass('ok');
		},
		// the errorPlacement has to take the layout into account
		errorPlacement: function(error, element) {
			error.insertAfter(element.parent().find('label:first'));
		},
		// specifying a submitHandler prevents the default submit, good for the demo
		submitHandler: function(form) {
			alert("Data submitted!");
			form.submit();
		},
		// set new class to error-labels to indicate valid fields
		success: function(label) {
			// set &nbsp; as text for IE
			label.html("&nbsp;").addClass("ok");
		}
	});
});